public class HashBucket {
    private static class Node {
        private int key;
        private int value;
        Node next;

        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }

    private Node[] array;
    private int size;   // 当前的数据个数
    private static final double LOAD_FACTOR = 0.75;
    private static final int DEFAULT_SIZE = 8;//默认桶的大小

    public void put(int key, int value) {
        // write code here
        int idx = key % array.length;
        Node node = new Node(key,value);

        Node cur = this.array[idx];
        while (cur != null) {
            if (cur.key == key) {
                cur.value = value;
                return;
            }
            cur = cur.next;
        }

        node.next = this.array[idx];
        this.array[idx] = node;
        this.size++;

        if (loadFactor() >= LOAD_FACTOR) resize();
    }


    private void resize() {
        // write code here
        Node[] tmp = new Node[this.array.length + (this.array.length >>> 1)];
        for (int i = 0; i < array.length; i++) {
            Node cur = this.array[i];
            while (cur != null) {
                Node curNext = cur.next;
                int idx = cur.key % tmp.length;
                cur.next = tmp[idx];
                tmp[idx] = cur;
                cur = curNext;
            }
        }
        this.array = tmp;
    }


    private double loadFactor() {
        return size * 1.0 / array.length;
    }


    public HashBucket() {
        // write code here
        this.array = new Node[DEFAULT_SIZE];

    }


    public int get(int key) {
        // write code here
        int idx = key % this.array.length;

        Node cur = this.array[idx];
        while (cur != null) {
            if (cur.key == key) {
                return cur.value;
            }
            cur = cur.next;
        }
        return -1;
    }
}